import React, { useEffect, useRef } from "react";

function useBounce(callBack, delay) {
  const timer = useRef(null);
  useEffect(() => {
    return () => {
      if (timer.current) {
        clearTimeout(timer.current);
      }
    };
  });

  return (...arg) => {
    if (timer.current) {
      clearTimeout(timer.current);
    }
    timer.current = setTimeout(() => {
      callBack(...arg);
    }, delay);
  };
}

export default useBounce;
